<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Alex
 * Created Time: 2019/7/3 10:36
 * Modified Time: 2019/7/3 10:36
 * Version: 2.0
 */
namespace App\Lib\Auth;
use App\Lib\Contracts\FLAuth as FLAuthContract;
use App\Modules\Employee\Services\AuthService;
use Illuminate\Support\Facades\Auth;
class FLAuth implements FLAuthContract
{
	private $authService;
	public function __construct(AuthService $authService)
	{
		$this->authService = $authService;
	}

	/**
     * 获取用户
     * @return \Illuminate\Contracts\Auth\Authenticatable
     */
    public function user()
    {
        return $this->guard()->user();
    }
    /**
     * 当前登录用户ID
     * @return int|string|null
     */
    public function id()
    {
        if ($this->guard()->user()) {
            return $this->user()->getAuthIdentifier();
        }
    }

	/**
	 * 当前店铺Id
	 */
	public function storeId(){
		return 1;
	}

    /**
     * 校验当前是否认证
     * @return boolean
     */
    public function check()
    {
        return $this->guard()->check();
    }
    /**
     * 当前角色,多个返回数组
     * @param int|null  $store 指定门店ID 默认当前门店
     * @return array|int|null
     */
    public function roles($store = null)
    {
    	$user = $this->user();
        return $this->authService->roles($user);
    }
    public function store()
    {
    	$userId = $this->id();
	    $storeId = $this->storeId();
    	return $this->authService->store($userId,$storeId);
    }

    public function isSuperBoss($store = null)
    {
	    $userId = $this->id();
	    $storeId = $this->storeId();
	    return $this->authService->isSuperBoss($userId,$storeId);
    }

    public function isBoss($store = null)
    {
	    $userId = $this->id();
	    $storeId = $this->storeId();
	    return $this->authService->isBoss($userId,$storeId);
    }

    public function isHolder($store = null)
    {
	    $userId = $this->id();
	    $storeId = $this->storeId();
	    return $this->authService->isHolder($userId,$storeId);
    }

    public function isBarber($store = null)
    {
	    $userId = $this->id();
	    $storeId = $this->storeId();
	    return $this->authService->isBarber($userId,$storeId);
    }

    public function storeList($role = [])
    {
	    $userId = $this->id();
	    $storeId = $this->storeId();
	    return $this->authService->store($userId,$storeId);
    }



	/**
     * 当前鉴权守护器
     * @return \Illuminate\Contracts\Auth\Guard
     */
    private function guard()
    {
        $auth = $this->authName();
        switch ($auth){
            case 'basic' :
                return Auth::guard('api');
                break;
            case 'wechat' :
                return Auth::guard('wx');
                break;
        }
    }
    private function authName()
    {
        $rout = $this->getRoute();
        return $rout->getAction()['providers'][0];
    }
    private function getRoute()
    {
        $rout = \request()->route();
        return $rout;
    }
}